﻿@using Blazorise.SpinKit
@using Dignite.FileExplorer.Files
@using Volo.Abp.AspNetCore.Components.Web.Theming.Layout
@using Dignite.Abp.BlazoriseUI.Components
@inherits AbpCrudPageBase<IFileDescriptorAppService,FileDescriptorDto, Guid, GetFilesInput, CreateFileInput, UpdateFileInput>
<Modal @ref="_modal" Closing="@ClosingModal">
    <ModalContent Size="ModalSize.ExtraLarge" Centered="true">
        <ModalHeader>
            <ModalTitle>@L["FileExplorer"]</ModalTitle>
            <CloseButton Clicked="CloseModal" />
        </ModalHeader>
        <ModalBody>            
            <Row>
                <Column ColumnSize="ColumnSize.Is3">
                    <AutoHeight ExtraHeight="240">
                        @if (DirectoryTreeComponentParameters!=null)
                        {
                            <DynamicComponent Type="@typeof(DirectoryTreeComponent)"
                                          Parameters="@DirectoryTreeComponentParameters" />
                        }
                    </AutoHeight>
                </Column>
                <Column ColumnSize="ColumnSize.Is9">
                    <Div Flex="Flex.JustifyContent.Between.AlignItems.Center" Margin="Margin.Is2.FromBottom">
                        <Div Padding="Padding.Is2.FromStart">
                            <Breadcrumb Margin="Margin.Is0.FromBottom">
                                @if (CurrentDirectory == null)
                                {
                                    <BreadcrumbItem>
                                        <BreadcrumbLink><Text>@L["AllFiles"]</Text></BreadcrumbLink>
                                    </BreadcrumbItem>
                                }
                                else
                                {
                                    <BreadcrumbItem>
                                        <BreadcrumbLink To="#" Clicked="@(()=>SelectedDirectoryChanged(null))">@L["AllFiles"]</BreadcrumbLink>
                                    </BreadcrumbItem>
                                    @foreach (var item in BreadcrumbItems)
                                    {
                                        <BreadcrumbItem>
                                            <Text>@item.Text</Text>
                                        </BreadcrumbItem>                                        
                                    }
                                    <BreadcrumbItem>
                                        <Text TextColor="TextColor.Secondary">@CurrentDirectory.Name</Text>
                                    </BreadcrumbItem>                                    
                                }
                            </Breadcrumb>                            
                        </Div>
                        <Div>
                            <FileEdit Changed="@OnFileChangedAsync" Disabled="@(Files!=null && Files.Any(f=>f.Status != FileUploadStatus.Fail))" Multiple="@Multiple" Filter="@Configuration?.AllowedFileTypeNames?.JoinAsString(",")">
                                <Feedback>
                                    <ValidationError />
                                </Feedback>
                            </FileEdit>
                        </Div>
                    </Div>
                    
                    @* ************************* DATA GRID ************************* *@
                    <ExtensibleDataGrid TItem="FileDescriptorDto" @ref="FileDataGridRef"
                                                                              SelectionMode="Multiple?DataGridSelectionMode.Multiple: DataGridSelectionMode.Single"
                                                                              Data="Entities"
                                                                              ReadData="OnDataGridReadAsync"
                                                                              TotalItems="TotalCount"
                                                                              ShowPager="true"
                                                                              PageSize="PageSize"
                                                                              CurrentPage="@CurrentPage"
                                                                              Columns="@FileTableColumns"
                                                                              ExtraHeight="340">
                    </ExtensibleDataGrid>
                </Column>
            </Row>

            @***********************  Upload File Panel  **************************@
            @if (Files != null && Files.Any())
            {
                <Div Position="Position.Fixed.Bottom.Is0.End.Is0" Shadow="Shadow.Large" Margin="Margin.Is5">
                    <Card>
                        <CardHeader>
                            @L["UploadFiles"]
                        </CardHeader>
                        <CardBody>
                            <Table>
                                <TableHeader>
                                    <TableRow>
                                        <TableHeaderCell>@L["FileName"]</TableHeaderCell>
                                        <TableHeaderCell>@L["FileSize"]</TableHeaderCell>
                                        <TableHeaderCell></TableHeaderCell>
                                    </TableRow>
                                </TableHeader>
                                <TableBody>
                                    @foreach (var file in Files)
                                    {
                                        <TableRow>
                                            <TableRowCell>
                                                @file.File.Name
                                                @if (file.Status == FileUploadStatus.Fail)
                                                {
                                                    <Paragraph TextColor="TextColor.Danger">
                                                        @file.ErrorMessage
                                                    </Paragraph>
                                                }
                                            </TableRowCell>
                                            <TableRowCell>@FileSizeFormatter.FormatSize(file.File.Size)</TableRowCell>
                                            <TableRowCell>                                                
                                                @switch (file.Status)
                                                {
                                                    case FileUploadStatus.Ready:
                                                        <Icon Name="IconName.MoreHorizontal" TextColor="TextColor.Secondary"></Icon>
                                                        break;
                                                    case FileUploadStatus.Progressing:
                                                        <SpinKit Type="SpinKitType.Chase" Size="16px" />
                                                        break;
                                                    case FileUploadStatus.Success:
                                                        <Icon Name="IconName.Check" TextColor="TextColor.Primary"></Icon>
                                                        break;
                                                    default:
                                                        <Icon Name="IconName.TimesCircle" TextColor="TextColor.Danger"></Icon>
                                                        break;
                                                }
                                            </TableRowCell>
                                        </TableRow>
                                    }
                                </TableBody>
                            </Table>
                        </CardBody>
                    </Card>
                </Div>
            }
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseModal">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="SelectFilesAsync">@L["Confirm"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>
